package com.editors;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.data.DoubleTextField;
import com.data.IntegerTextField;
import com.data.LineData;
import com.data.Point3D;
import com.data.PolygonMesh;

public class TrackEditorTemplate extends JDialog implements ActionListener{

	int WIDTH=300;
	int BOTTOM_HEIGHT=100;
	int HEIGHT=200;
	private JButton generate;
	private JButton delete;
	
	
	int TEMPLATE_TYPE_CIRCLE=0;
	private int TEMPLATE_TYPE=TEMPLATE_TYPE_CIRCLE;
	
	private JTabbedPane jtb;
	private DoubleTextField circle_radius;
	private DoubleTextField circle_center_x;
	private DoubleTextField circle_center_y;
	private IntegerTextField num_tracks;
	
	PolygonMesh pm=null;
	
	public TrackEditorTemplate(){
		
		pm=null;
	
		setLayout(null);
		setModal(true);
		setTitle("Choose template");
		setSize(WIDTH,HEIGHT+BOTTOM_HEIGHT);
		
		jtb=new JTabbedPane();
		jtb.setBounds(0, 0, WIDTH, HEIGHT);
        add(jtb); 
        jtb.add("Circle",getCirclePanel());
		
        generate=new JButton("generate template");
        generate.setBounds(10,HEIGHT+10,150,20);
        generate.addActionListener(this);
        add(generate);
        delete=new JButton("Cancel");
        delete.setBounds(180,HEIGHT+10,100,20);
        delete.addActionListener(this);
        add(delete);
		
		setVisible(true);
	
	}

	private Component getCirclePanel() {
	
		
		
		JPanel circle=new JPanel();
		circle.setLayout(null);
		
		int r=10;
		int column=100;
		
		
		JLabel jlb=new JLabel("#Tracks");
		jlb.setBounds(5, r, 100, 20);
		circle.add(jlb);

		num_tracks=new IntegerTextField();
		num_tracks.setBounds(column, r, 100, 20);
		circle.add(num_tracks);

		r+=30;

		jlb=new JLabel("Radius");
		jlb.setBounds(5, r, 100, 20);
		circle.add(jlb);

		circle_radius=new DoubleTextField();
		circle_radius.setBounds(column, r, 100, 20);
		circle.add(circle_radius);

		r+=30;
		
		jlb=new JLabel("Center X");
		jlb.setBounds(5, r, 100, 20);
		circle.add(jlb);

		circle_center_x=new DoubleTextField();
		circle_center_x.setBounds(column, r, 100, 20);
		circle.add(circle_center_x);

		r+=30;
		
		
		jlb=new JLabel("Center Y");
		jlb.setBounds(5, r, 100, 20);
		circle.add(jlb);

		circle_center_y=new DoubleTextField();
		circle_center_y.setBounds(column, r, 100, 20);
		circle.add(		circle_center_y);

		r+=30;

		
		
		return circle;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
	
		

		Object o=e.getSource();

		if(o==generate){
			
			generateTemplate();
			dispose();	
		}
		else if(o==delete){

			TEMPLATE_TYPE=-1;
			pm=null;
			dispose();	
		}
		
	}

	private void generateTemplate() {
	
		
		if(TEMPLATE_TYPE==TEMPLATE_TYPE_CIRCLE){
			
			String snum_tracks=num_tracks.getText();
			String scircle_radius=circle_radius.getText();
			String scircle_center_x=circle_center_x.getText();
			String scircle_center_y=circle_center_y.getText();

			if(snum_tracks==null || snum_tracks.length()==0 || 
					scircle_radius==null || scircle_center_x.length()==0 
					|| scircle_center_x==null || scircle_center_x.length()==0
					|| scircle_center_y==null || scircle_center_y.length()==0
			)
				return ;

			int numTracks=num_tracks.getvalue();
			double cr=circle_radius.getvalue();
			double cx=circle_center_x.getvalue();
			double cy=circle_center_y.getvalue();
			
			
			double teta=2*Math.PI/(numTracks);
			
			pm=new PolygonMesh();
			
			pm.points=new Point3D[numTracks];
			pm.polygonData=new Vector();			
		
			
			for (int i = 0; i < numTracks; i++) {
				
				LineData ld=new LineData();
				ld.addIndex(i);
				ld.addIndex((i+1)%numTracks);
				
				pm.polygonData.add(ld);
				
				
				double angle=teta*i;
				double x=cr*Math.cos(angle)+cx;
				double y=cr*Math.sin(angle)+cy;
				
				pm.points[i]=new Point3D(x,y,0);
				
			}

			
		}
		
	}
	
	public PolygonMesh getTemplate(){
		
		
		if(TEMPLATE_TYPE==-1) 
			return null;
		

		
	
	     return pm;
	}	
}
